package requrest

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"math"
)

type Pagination struct {
	Limit     int    `json:"limit" form:"limit" uri:"limit"`
	Page      int    `json:"page" form:"page" uri:"page"`
	Sort      string `json:"sort" form:"sort" uri:"sort"`
	PageCount int    `json:"page_count"`
}

func GeneratePaginationFormRequest(c *gin.Context) (pagination Pagination) {
	if err := c.ShouldBind(&pagination); err != nil {
		fmt.Printf("参数绑定错误%s", err)
	}
	if pagination.Limit <= 0 {
		pagination.Limit = 10
	}
	if pagination.Page < 1 {
		pagination.Page = 1
	}
	if len(pagination.Sort) == 0 {
		pagination.Sort = "created_at desc"
	}
	return
}

func (pagination *Pagination) GetPageCount(count int64) {
	if count <= 0 {
		pagination.PageCount = 0
	} else {
		pageCount := float64(count) / float64(pagination.Limit)
		pagination.PageCount = int(math.Ceil(pageCount))
	}
}
